/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bashe.common.config;

import han.bashe.common.exception.BasheErrorCode;
import han.bashe.common.exception.BasheException;
import han.bashe.common.jms.JmsBindingWrapper;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlOptions;

import v1.config.bashe.han.BusConfigDocument;
import v1.config.bashe.han.BusConfigDocument.BusConfig;
import v1.config.bashe.han.BusConfigDocument.BusConfig.JmsSenders;
import v1.config.bashe.han.BusConfigDocument.BusConfig.StatisticsLog;
import v1.config.bashe.han.BusConfigDocument.BusConfig.JmsSenders.Sender;

/**
 * @Author Kent
 */
public class Config {
	private static Log logger = LogFactory.getLog(Config.class);

	private static BusConfigDocument configDoc;
	
	private static String currentBusId;

	private static JmsBindingWrapper statisticsJmsBinding;
	
	public static int DEFAULT_BIFANG_TIMEOUT = 60;
	
	public static int DEFAULT_SERVICE_TIMEOUT = 60;
	
	public static boolean cacheEnable = true;
	
	public static String notifyServiceCode = null;
	
	
	
	
	
	public static HashMap<String,Sender> jmsSenderConfig = new HashMap<String, Sender>();
	
	public static void init() throws BasheException {
		logger.info("Init begin ......");
		InputStream in = null;
		try {
			
			
			try {
				in = new FileInputStream("bus-config.xml");
			} catch (Exception e) {
				in = null;
			}

			if (in == null) {
				logger
						.error("Parsing bus-config.xml error. bus-config.xml should be put in your domain root");
				throw new BasheException(
						BasheErrorCode.PARSE_CONFIG_XML_ERROR,
						"Parsing bus-config.xml error. bus-config.xml should be put in your domain root");
			}
			XmlOptions validateOptions = new XmlOptions();
			ArrayList<XmlError> errorList = new ArrayList<XmlError>();
			validateOptions.setErrorListener(errorList);

			configDoc = BusConfigDocument.Factory.parse(in);

			boolean valid = configDoc.validate(validateOptions);
			if (!valid) {
				for (int i = 0, j = errorList.size(); i < j; i++) {
					XmlError err = (XmlError) errorList.get(i);
					logger.error(err.getMessage());
					logger.error(("Location of invalid XML: " + err
							.getCursorLocation().xmlText()));

				}
				throw new BasheException(BasheErrorCode.PARSE_CONFIG_XML_ERROR,
						"");
			}
			JmsSenders ss = configDoc.getBusConfig().getJmsSenders();
			for(Sender s : ss.getSenderList()){
				jmsSenderConfig.put(s.getAppServer() + "|" + s.getVersion(), s);
				
			}

		} catch (Throwable e) {

			logger
					.error(
							"Parsing bus-config.xml error. Please check the bus-config.xml and restart weblogic",
							e);
			throw new BasheException(
					BasheErrorCode.PARSE_CONFIG_XML_ERROR,
					"Parsing bus-config.xml error. bus-config.xml should be put in your domain root");
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (Exception e) {
			}
		}

	}

	public static BusConfig getConfig() {
		if (configDoc == null) {
			try {
				init();
			} catch (Exception e) {
			}
			return configDoc.getBusConfig();
		} else {
			return configDoc.getBusConfig();

		}
	}

	public static String getCurrentBusId() {
		return currentBusId;
	}

	public static void setCurrentBusId(String currentBusId) {
		Config.currentBusId = currentBusId;
	}
	
	public static boolean isStatisticsLogOn(){
		if(configDoc.getBusConfig().getStatisticsLog() == null)
			return true;
		if(configDoc.getBusConfig().getStatisticsLog().intValue() == StatisticsLog.INT_ON){
			return true;
		}
		return false;
	}

	public static JmsBindingWrapper getStatisticsJmsBinding() {
		return statisticsJmsBinding;
	}

	public static void setStatisticsJmsBinding(JmsBindingWrapper statisticsJmsBinding) {
		Config.statisticsJmsBinding = statisticsJmsBinding;
	}

	public static String getNotifyServiceCode() {
		return notifyServiceCode;
	}

	public static void setNotifyServiceCode(String notifyServiceCode) {
		Config.notifyServiceCode = notifyServiceCode;
	}
	
	
}
